package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.TeacherUpdateDto;
import com.tcm.entity.resp.R;
import com.tcm.exception.DatabaseOperationException;
import com.tcm.service.TeacherService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 教师管理控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/7/2 16:17
 */
@Slf4j
@RestController
@RequestMapping("/pc/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    /**
     * 添加教师信息接口
     * @param teacherUpdateDto 教师信息封装对象
     * @param request http请求对象
     * @return 添加结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addTeacherInfo(@RequestBody @Valid TeacherUpdateDto teacherUpdateDto, HttpServletRequest request){
        String accessToken = AccessTokenUtil.getAccessToken(request);
        if (teacherService.addTeacherInfo(teacherUpdateDto, accessToken)) {
            return R.success();
        }else {
            return R.fail(ResponseEnum.HANDLE_FAIL);
        }
    }

    /**
     * 获取教师列表
     * @return 教师列表
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getTeacherList(){
        return R.success(teacherService.getTeacherList());
    }

    /**
     * 删除教师信息
     * @param teacherId 教师ID
     * @param request http请求对象
     * @return 删除结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R dropTeacherInfo(String teacherId, HttpServletRequest request){
        // 判断参数是否为空
        if (!StringUtils.hasText(teacherId)) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        // 调用业务方法

        boolean res = false ;
        try {
            res = teacherService.removeTeacherById(AccessTokenUtil.getUserId(request), teacherId);
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return res ? R.success(null) : R.fail(ResponseEnum.USER_NOT_EXIST);
    }

    /**
     * 批量删除教师信息接口
     * @param ids 教师ID集合
     * @param request http请求对象
     * @return 删除结果
     */
    @RequestMapping(value = "/batch", method = RequestMethod.DELETE)
    public R dropTeacherInfoBatch(@RequestParam("ids") List<String> ids, HttpServletRequest request){
        Long userId = AccessTokenUtil.getUserId(request);
        boolean res = false;
        try {
            res = teacherService.removeTeacherBatchById(ids, userId);
        } catch (DatabaseOperationException e) {
            R.fail(0, e.getMessage());
        }
        return res ? R.success() : R.fail(0, "教师ID错误或用户不存在");
    }

    /**
     * 更新教师信息
     * @param teacherUpdateDto 教师更新信息封装对象
     * @param request http请求对象，用于获取当前登录用户
     * @return 更新结果
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R modifyTeacherInfo(@RequestBody @Valid TeacherUpdateDto teacherUpdateDto, HttpServletRequest request){
        boolean res = false;
        try {
            res = teacherService.modifyTeacherInfo(teacherUpdateDto, AccessTokenUtil.getUserId(request));
        }catch (DatabaseOperationException e) {
            return R.fail(0, e.getMessage());
        }
        if (!res) {
            return R.fail(ResponseEnum.USER_NOT_EXIST);
        }
        return R.success();
    }

}
